<?php
if(!defined('IN_ROOT') || !isset($php_self) || !preg_match("/[\/\\\\]index\.php$/", $php_self)) {
	exit('Access Denied');
}

//权限检查
permission();

// 检查站点名字是否符合逻辑
function checksitename($sitename) {
	if(!$sitename || strlen($sitename) > 30) {
		$result = '站点名不能空并不能大于30个字符<br />';
		return $result;
	}
	elseif(eregi("[<>{}(),%#|^&!`$]",$sitename)) {
		$result = '站点名中不能含有特殊字符<br />';
		return $result;
	}
}

// 检查站点描述是否符合逻辑
function checknote($note = '') {
	if($note && strlen($note) > 200) {
		$result = '站点描述不能大于200个字符<br />';
		return $result;
	}
}

//添加站点

if($_POST['action'] == 'doaddsite') {
  	$site_title = trim($_POST["title"]);
  	$site_site_url = trim($_POST["url"]);
  	$site_rss_url = trim($_POST["rss_url"]);
  	$site_icon = trim($_POST["icon"]);
  	$rss_cate = trim($_POST["rss_cate"]);
  	$site_email = trim($_POST["email"]);
 	$site_count = trim($_POST["count"]);
  	$site_content = trim($_POST["content"]);
	$state = intval($_POST['state']);

	$result  = checksitename($site_title);

	$result .= checkurl($site_site_url,0);
	$result .= checkurl($site_rss_url,0);
	$result .= checkurl($site_icon,0);

	$result .= checknote($content);

	if($result) {
		redirect($result);
	}
	$site_title    = char_cv($site_title);
	$site_site_url     = char_cv($site_site_url);
	$site_rss_url     = char_cv($site_rss_url);
	$site_icon     = char_cv($site_icon);
	$site_content    = char_cv($site_content);

	$rs = $db->fetch_one_array("SELECT count(*) AS sites FROM {$tablepre}xna_site WHERE site_title='$site_title' AND site_url='$site_url'");
    if($rs['sites'])	{
        redirect('该站点在数据库中已存在', 'index.php?job=site');

    }
  $query = $db->query("INSERT INTO {$tablepre}xna_site
   		(site_title,site_url,rss_url,site_icon,rss_cate,site_email,site_count,site_content,state,addtime)
	VALUES
		 ('$site_title','$site_site_url','$site_rss_url','$site_icon','$rss_cate','$site_email','$site_count','$site_content','$state','$timestamp')");
	categories_recache();
    redirect('添加站点成功', 'index.php?job=site');

}
//修改站点

if($action == 'domodsite') {
	$site_title = trim($_POST["title"]);
  	$site_url = trim($_POST["url"]);
  	$site_rss_url = trim($_POST["rss_url"]);
  	$site_icon = trim($_POST["icon"]);
  	$rss_cate = trim($_POST["rss_cate"]);
  	$site_email = trim($_POST["email"]);
  	$site_count = trim($_POST["count"]);
  	$site_content = trim($_POST["content"]);
  	$state = intval($_POST['state']);
	$cid    = intval($_POST['cid']);
	$result = checksitename($site_title);

	if($result)	{redirect($result);}

	$site_title = char_cv($site_title);
	$rs = $db->fetch_one_array("SELECT count(*) AS categories FROM {$tablepre}xna_category WHERE cid!='$cid' AND cate_title='$site_title'");
    if($rs['categories']) {
		redirect('已经有其他站点使用'.$site_title.'这个名称');
    }

	// 更新站点
   $db->query("UPDATE {$tablepre}xna_site SET
		    site_title='$site_title' ,
		    site_url='$site_url' ,
		    rss_url='$site_rss_url' ,
		    site_icon='$site_icon' ,
		    rss_cate='$rss_cate',
		    site_email='$site_email',
		    site_count='$site_count',
		    site_content='$site_content',
		    state='$state'
		WHERE siteid='$sid'");
	categories_recache();

    redirect('修改站点成功', 'index.php?job=site');
}

//批量处理

if($_POST['action'] == 'domoresite') {
	if($ids = implode_ids($_POST['delete'])) {
		$db->query("DELETE FROM	{$tablepre}xna_site WHERE siteid IN ($ids)");
	}
	if(is_array($_POST['displayorder'])) {
		foreach($_POST['displayorder'] as $siteid => $value) {
			$db->query("UPDATE {$tablepre}xna_site SET displayorder='".intval($_POST['displayorder'][$siteid])."', state='".intval($_POST['state'][$siteid])."' WHERE siteid='".intval($siteid)."'");
			$db->query("UPDATE {$tablepre}xna_feed SET state='".intval($_POST['state'][$siteid])."' WHERE siteid='".intval($siteid)."'");
		}
	}
	categories_recache();

    redirect('站点已成功更新', 'index.php?job=site');

}


if(!$action) {
	$action = 'list';
}
//站点操作
if (in_array($action, array('addsite', 'modsite', 'delcate','dofetch'))) {
	if ($action == 'addsite') {
		$subnav = '添加站点';
        $site["cid"]=intval($_GET['cid']);
	} elseif($action == 'dofetch'){
		$subnav = '聚合站点';
		$sid=intval($_GET['sid']);
		ob_start();
		doFetch($sid);
		redirect('更新成功', 'index.php?job=site');
		}else {
		$site = $db->fetch_one_array("SELECT * FROM {$tablepre}xna_site WHERE siteid='".intval($_GET['sid'])."'");
		if($action == 'modsite') {
			$subnav = '编辑站点';
		} else {
			$subnav = '删除站点';
		}
	}
}
// 导入OPML
if ($action == 'importopml') {
	$cid = intval($_POST['cid']);
	if (!$cid) {
		redirect('请选择目标分类', 'index.php?job=site&action=opmlimport');
	}
	$uid = intval($_POST['uid']);
	//if (!$uid) {
	//	redirect('请选择文章作者', 'index.php?job=site&action=opmlimport');
	//}
	$xmlfile = $_FILES['opmlfile'];
	if (is_array($xmlfile)) {
		$attachment      = $xmlfile['tmp_name'];
		$attachment_name = $xmlfile['name'];
		$attachment_size = $xmlfile['size'];
		$attachment_type = $xmlfile['type'];
	}
	if (trim($attachment) != 'none' && trim($attachment) != '' && trim($attachment_name) != '') {
		$rssinfo = pathinfo($attachment_name);
		if ($rssinfo['extension'] == 'xml') {
			$attachment = upfile($attachment, _ROOT.'cache/opml_tmp.xml');
			// 如果一种函数上传失败，还可以用其他函数上传
			if (!$attachment) {
				redirect('上传XML文件发生意外错误!');
			}
			$opml = file_get_contents(_ROOT .'cache/opml_tmp.xml') ;
			require_once(_ROOT .'include/opml.class.php');
			$i = 0;
			if (is_array($feeds)) {
				foreach ($feeds as $rss) {
					$i++;
					$db->query("INSERT INTO {$tablepre}xna_site (rss_cate, uid, site_title, site_url,site_content, addtime) VALUES ('$cid', '$uid', '".$rss['site_title']."', '".$rss['site_url']."','".$rss['site_content']."', '".$timestamp."')");
				}
			}
			@unlink($attachment);

			$db->query("UPDATE {$tablepre}xna_category SET site_conut=site_conut+$i WHERE cid='$cid'");
			$db->query("UPDATE {$tablepre}xna_statistics SET site_conut=site_conut+$i");
			sites_recache();
			categories_recache();
			statistics_recache();

			redirect('导入OPML数据成功', 'index.php?job=site');
		} else {
			redirect('只允许上传XML格式的文件', 'index.php?job=site&action=opmlimport');
		}
	} else {
		redirect('请选择要上传的XML文件', 'index.php?job=site&action=opmlimport');
	}
}
if ($action == 'list') {
	if($page) {
		$start_limit = ($page - 1) * 30;
	} else {
		$start_limit = 0;
		$page = 1;
	}
	$tatol = $stats['site_count'];	
	$query = $db->query("SELECT * FROM {$tablepre}xna_site ORDER BY siteid DESC LIMIT $start_limit, 30");
	$multipage = multi($tatol, 30, $page, 'index.php?job=site&action=list'.$pagelink);
	$sitedb = array();
	while ($site = $db->row($query)) {
		if ($site['state'] == '1') {
			$site['state'] = '<option value="1" selected>显示</option><option value="0">隐藏</option>';
		} else {
			$site['state'] = '<option value="1">显示</option><option value="0" selected>隐藏</option>';
		}
		$site[addtime] = date("Y-m-d ",$site["addtime"]);
		$site[uptime] = date("Y-m-d h:m:s",$site["uptime"]);
		$sitedb[] = $site;
	}
	unset($site);
	$db->free($query);
	$subnav = '编辑站点';
}

$navlink_L = ' &raquo; <a href="index.php?job=site">站点管理</a>'.($subnav ? ' &raquo; '.$subnav : '');
cpheader();
include template("site", _ROOT . "/template/admin/");

?>